package com.reger.core.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Component;

import com.reger.core.Context;

@Component
public class HttpOutJson {

	private static final Logger logger = LoggerFactory.getLogger(HttpOutJson.class);
	private static AbstractJackson2HttpMessageConverter jsonConverter;

	@Autowired 
	public void setJsonConverter(AbstractJackson2HttpMessageConverter jsonConverter) {
		HttpOutJson.jsonConverter = jsonConverter;
	}
	public static boolean isOutEd() {
		return null!=Context.getForServer("http is out ed !");
	}
	/**
	 * 响应json
	 * @param model 响应的对象
	 */
	public static void out(Object model){
		out(model, 200);
	}
	/**
	 * 响应json
	 * @param model 响应的对象
	 * @param status 响应的状态码
	 */
	public static void out(Object model, int status) {
		try {
			if(isOutEd()){
				logger.warn("0. 第二次尝试http输出‘{}’ ", model );
				return ;
			}
			Context.put("http is out ed !",true);
			HttpServletResponse response=Context.getResponse();
			response.setStatus(status);
			jsonConverter.write(model, MediaType.APPLICATION_JSON, new ServletServerHttpResponse(response));
		} catch (HttpMessageNotWritableException | IOException e) {
			logger.warn("1. 输出资源‘{}’异常 ：{}", model,e.getMessage());
		}catch (Exception e) {
			logger.warn("2.  输出资源‘{}’异常：{}", model,e.getMessage());
		}
	}
}
